/**
 * 时间戳转日期
 */
export const formatDateTime = (date, fmt) => {
  function padLeftZero(str) {
    return ('00' + str).substr(str.length);
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
}

/**传入siaRequestData（body），递归解析带描述的json**/
export const jsonMain = (dataList) =>{
  let json = {};
  dataList = JSON.parse(JSON.stringify(dataList));
  if (!dataList.leaf) {
    json["<span class='name'>" + dataList.name + "<input type='hidden' value='" + dataList.name + "' dataType='" + dataList.type + "'></span><span class='type'>(" + dataList.type + ")</span>"] = jsonBind(dataList.detail, dataList.name);
  } else {
    json["<span class='name'>" + dataList.name + "<input type='hidden' value='" + dataList.name + "' dataType='" + dataList.type + "'></span><span class='type'>(" + dataList.type + ")</span>"] = dataList.desc ? dataList.desc : "暂无字段的描述注解";
  }
  return json;
}

/**递归生成解析子节点json**/
const jsonBind = (detail, parentKey) => {
  parentKey = parentKey ? parentKey + "." : "";
  let tempJson = {};
  for (let i = 0; i < detail.length; i++) {
    if (!detail[i].leaf) {
      tempJson["<span class='name'>" + detail[i].name + "<input type='hidden' value='" + parentKey + detail[i].name + "' dataType='" + detail[i].type + "'></span><span class='type'>(" + detail[i].type + ")</span>"] = jsonBind(detail[i].detail, detail[i].name);
    } else {
      tempJson["<span class='name'>" + detail[i].name + "<input type='hidden' value='" + parentKey + detail[i].name + "' dataType='" + detail[i].type + "'></span><span class='type'>(" + detail[i].type + ")</span>"] = detail[i].desc ? detail[i].desc : "暂无字段的描述注解";
    }
  }
  return tempJson;
}

/**多个body的情况下，循环调用就可以了**/
export const jsonBody = (data) => {
  let json = {}
  data = JSON.parse(JSON.stringify(data));
  json["<span class='name'>" + data.name + "<input type='hidden' value='" + data.name + "' dataType='" + data.type + "'></span><span class='type'>(" + data.type + ")</span>"] = data.desc ? data.desc : "暂无字段的描述注解";
  delete data.leaf;
  delete data.name;
  delete data.desc;
  delete data.type;
  delete data.httpType;
  delete data.isLeaf;
  delete data.parameterizedIndex;
  delete data.parameterizedList;
  delete data.typeDesc;
  return json;
}

/**验证是否包含数据类型**/
export const toContainJavaType = (data) => {
  let dataTypeList = ["STRING", "DATE", "INTEGER", "INT", "DOUBLE", "CHAR", "BOOLEAN", "FLOAT"];
  for (let i = 0; i < dataTypeList.length; i++) {
    if (dataTypeList[i] === data.toUpperCase()) {
      return true;
      break;
    }
  }
  return false;
}

/**数据转换**/
export const toDataType = (type) => {
  type = type.toUpperCase();
  let dataTypeList = ["INTEGER", "INT", "DOUBLE", "FLOAT", "FLOAT", "NUMBER"];
  if (dataTypeList.indexOf(type) >= 0) {
    return "NUMBER";
  }
  else if (type === "STRING") {
    return "STRING";
  }
  else if (type === "BOOLEAN") {
    return "BOOLEAN";
  }
  else {
    return "OBJECT";
  }
}

Date.prototype.Format = function(fmt)
{
  let o = {
    "M+" : this.getMonth()+1,                 //月份
    "d+" : this.getDate(),                    //日
    "h+" : this.getHours(),                   //小时
    "m+" : this.getMinutes(),                 //分
    "s+" : this.getSeconds(),                 //秒
    "q+" : Math.floor((this.getMonth()+3)/3), //季度
    "S"  : this.getMilliseconds()             //毫秒
  };
  if(/(y+)/.test(fmt))
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  for(var k in o)
    if(new RegExp("("+ k +")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  return fmt;
}
